package logging

import (
	"geekbang-go-homework/week04/pkg/middleware"
	"net/http"
	"time"

	"google.golang.org/grpc/grpclog"
)

func Logging(log grpclog.LoggerV2) middleware.HandlerFunc {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
			s := time.Now()
			h.ServeHTTP(writer, request)
			t := time.Since(s)
			log.Infof("[%s] %s %s", request.Method, request.RequestURI, t)
		})
	}
}
